package com.jf3q.iot.job;

import com.jf3q.iot.domain.IotDevice;
import com.jf3q.iot.domain.IotGateway;
import com.jf3q.iot.service.IotDeviceService;
import com.jf3q.iot.service.IotGatewayService;
import com.jf3q.iot.service.SendInstructsService;
import com.jf3q.iot.utils.ElectricityMeterUtil;
import com.jf3q.iot.utils.InstructConstant;
import com.jf3q.iot.utils.InstructTypeConstant;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:xiaojie
 * @create: 2023-06-06 19:56
 * @Description: 抄表定时任务-每两小时抄一次
 */
@Slf4j
public class ElectricityMeterJob extends QuartzJobBean {
    @Resource
    IotDeviceService iotDeviceService;
    @Resource
    IotGatewayService gatewayService;
    @Resource
    SendInstructsService sendInstructsService;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("查出所有的电表设备的余额");
        //查出所有的电表设备的余额
        List<IotDevice> list = iotDeviceService.list();
        for (IotDevice device : list) {
            //查出网关
            IotGateway gatewayVO = gatewayService.getById(device.getGatewayId());
            String address = ElectricityMeterUtil.deviceAddressConvert(device.getDeviceCode());
            String ip = "/"+gatewayVO.getGatewayIp()+":"+gatewayVO.getGatewayPort();
            String instruce = ElectricityMeterUtil.getCompleteInstruct(address, InstructConstant.ELECTRICITY_BALANCE);
            sendInstructsService.sendInstructs(instruce,ip,device.getDeviceCode(), InstructTypeConstant.E_BALANCE_SETUPA.getValue());

    }


    }
}
